import json
import pyecharts.options as opts
from pyecharts.charts import Line

"""
堆积图内容：
-报告名称：REPORT_DATE_NAME
-货币资金     MONETARYFUNDS
-存货   INVENTORY
-其他流动资产 OTHER_CURRENT_ASSET
-长期股权投资 LONG_EQUITY_INVEST
-固定资产 FIXED_ASSET
-长期待摊费用 LONG_PREPAID_EXPENSE
-应收票据及应收账款 NOTE_ACCOUNTS_RECE
-无形资产 INTANGIBLE_ASSET
-在建工程 CIP
"""




def get_test_data():
    with open("zichan_data.json","r") as fd:
        filedata = fd.read()
        json_data = json.loads(filedata)
        return json_data

def get_data(data, key):
    value = data.get(key)
    if value is None:
        value = 0
    return value


def make_xdata(data, fieldname):
    result = []
    count = data["result"]["count"]
    _data = data["result"]["data"]
    for _ in _data:
        result.append(get_data(_,fieldname))
    result.reverse()
    return result


# def make_xdata(data, fieldname):
#     result = []
#     count = data["result"]["count"]
#     _data = data["result"]["data"]
#     for _ in _data:
#         result.append(_[fieldname])
#     result.reverse()
#     return result


def make_stack_data(keyname, data):
    result = []
    return result


def makechart_zichan(data_zichan):
    x_data = make_xdata(data_zichan, "REPORT_DATE_NAME")
    # y_data = [820, 932, 901, 934, 1290, 1330, 1320]
    (
        Line()
        .add_xaxis(xaxis_data=x_data)
        .add_yaxis(
            series_name="货币资金",
            stack="总量",
            y_axis=make_xdata(data_zichan, "MONETARYFUNDS"),
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=False),
        )
        .add_yaxis(
            series_name="存货",
            stack="总量",
            y_axis=make_xdata(data_zichan, "OTHER_CURRENT_ASSET"),
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=False),
        )
        .add_yaxis(
            series_name="其他流动资产",
            stack="总量",
            y_axis=make_xdata(data_zichan, "INVENTORY"),
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=False),
        )
        .add_yaxis(
            series_name="长期股权投资",
            stack="总量",
            y_axis=make_xdata(data_zichan, "LONG_EQUITY_INVEST"),
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=False),
        )
        .add_yaxis(
            series_name="固定资产",
            stack="总量",
            y_axis=make_xdata(data_zichan, "FIXED_ASSET"),
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=True, position="top"),
        )
        .add_yaxis(
            series_name="长期待摊费用",
            stack="总量",
            y_axis=make_xdata(data_zichan, "LONG_PREPAID_EXPENSE"),
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=True, position="top"),
        )

        .add_yaxis(
            series_name="应收票据及应收账款",
            stack="总量",
            y_axis=make_xdata(data_zichan, "NOTE_ACCOUNTS_RECE"),
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=False),
        )
        .add_yaxis(
            series_name="无形资产",
            stack="总量",
            y_axis=make_xdata(data_zichan, "INTANGIBLE_ASSET"),
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=True, position="top"),
        )
        .add_yaxis(
            series_name="在建工程",
            stack="总量",
            y_axis=make_xdata(data_zichan, "CIP"),
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=True, position="top"),
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="资产堆叠图"),
            tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
            yaxis_opts=opts.AxisOpts(
                type_="value",
                axistick_opts=opts.AxisTickOpts(is_show=False),
                splitline_opts=opts.SplitLineOpts(is_show=False),
            ),
            xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
        )
        .render("资产堆积图.html")
    )


"""
#短期借款 SHORT_LOAN
#长期借款 LONG_LOAN
#应付票据及应付账款 NOTE_ACCOUNTS_PAYABLE
#应付职工薪酬 STAFF_SALARY_PAYABLE
#预收款项 ADVANCE_RECEIVABLES
#合同负债 CONTRACT_LIAB
#其他应付款合计 TOTAL_OTHER_PAYABLE
"""


def makechart_fuzhai(data_zichan):
    x_data = make_xdata(data_zichan, "REPORT_DATE_NAME")
    (
        Line()
        .add_xaxis(xaxis_data=x_data)
        .add_yaxis(
            series_name="短期借款",
            stack="总量",
            y_axis=make_xdata(data_zichan, "SHORT_LOAN"),
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=False),
        )
        .add_yaxis(
            series_name="长期借款",
            stack="总量",
            y_axis=make_xdata(data_zichan, "LONG_LOAN"),
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=False),
        )
        .add_yaxis(
            series_name="应付票据及应付账款",
            stack="总量",
            y_axis=make_xdata(data_zichan, "NOTE_ACCOUNTS_PAYABLE"),
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=False),
        )
        .add_yaxis(
            series_name="应付职工薪酬",
            stack="总量",
            y_axis=make_xdata(data_zichan, "STAFF_SALARY_PAYABLE"),
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=False),
        )
        .add_yaxis(
            series_name="预收款项",
            stack="总量",
            y_axis=make_xdata(data_zichan, "ADVANCE_RECEIVABLES"),
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=True, position="top"),
        )
        .add_yaxis(
            series_name="合同负债",
            stack="总量",
            y_axis=make_xdata(data_zichan, "CONTRACT_LIAB"),
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=False),
        )
        .add_yaxis(
            series_name="其他应付款合计",
            stack="总量",
            y_axis=make_xdata(data_zichan, "TOTAL_OTHER_PAYABLE"),
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=True, position="top"),
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="负债堆叠图"),
            tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
            yaxis_opts=opts.AxisOpts(
                type_="value",
                axistick_opts=opts.AxisTickOpts(is_show=False),
                splitline_opts=opts.SplitLineOpts(is_show=False),
            ),
            xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
        )
        .render("负债堆积图.html")
    )


if __name__ == '__main__':
    data = get_test_data()
    makechart_fuzhai(data)

